﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace FWF.Logging.Viewer
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmLogFilter : Form
    {

        #region Constructors / Finalizer

        /// <summary>
        /// Initializes a new instance of the <see cref="frmLogFilter"/> class.
        /// </summary>
        public frmLogFilter()
        {
            this.Filters = new List<LogFilterItem>();

            InitializeComponent();
        }

        #endregion

        /// <summary>
        /// Gets or sets the filters.
        /// </summary>
        /// <value>The filters.</value>
        public List<LogFilterItem> Filters
        {
            get;
            set;
        }

        /// <summary>
        /// Handles the Load event of the frmFilter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void frmFilter_Load( object sender, EventArgs e )
        {
            //
            gridFilters.Rows.Clear();

            //
            Filters.ForEach(
                x =>
                    gridFilters.Rows.Add(
                        x.Category,
                        x.LogPrefix
                    )
                );
        }

        /// <summary>
        /// Gets the new settings.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<LogFilterItem> GetFilters()
        {
            foreach ( DataGridViewRow row in gridFilters.Rows )
            {
                LogFilterItem filter = new LogFilterItem();
                filter.Category = row.Cells[0].Value as string;
                filter.LogPrefix = row.Cells[1].Value as string;

                if (
                    string.IsNullOrEmpty(filter.Category) &&
                    string.IsNullOrEmpty(filter.LogPrefix)
                    )
                {
                    continue;
                }

                if (filter.Category == null)
                {
                    filter.Category = string.Empty;
                }
                if (filter.LogPrefix == null)
                {
                    filter.LogPrefix = string.Empty;
                }
                
                yield return filter;
            }
            
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click( object sender, EventArgs e )
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

    }
}
